When should an array name be treated as a pointer and when does it just represent the array itself? [duplicate]

Posted by user1087373 on Stack Overflow See other posts from Stack Overflow or by user1087373
Published on 2014-06-05T09:07:31Z Indexed on 2014/06/05 9:25 UTC
Read the original article Hit count: 234

Filed under:
|
|

This question already has an answer here:

I just made a test program after reading the book and the result turned out confusing:

    #include <stdio.h>
    int main(void)
    {
        char text[] = "hello!";

        printf("sizeof(text):%d  sizeof(text+2):%d  sizeof(text[0]):%d \n",(int)sizeof(text), sizeof(text+2), sizeof(text[0]));
        printf("text:%p sizeof(text):%d      &text:%p sizeof(&text):%d \n",text, sizeof(text), &text, sizeof(&text));
        printf("text+1:%p  &text+1:%p  \n", text+1, &text+1);

        return 0;
    }

The result:

sizeof(text):7  sizeof(text+2):4  sizeof(text[0]):1 
text:0xbfc8769d sizeof(text):7      &text:0xbfc8769d sizeof(&text):4 
text+1:0xbfc8769e  &text+1:0xbfc876a4 

What makes me feel confused are:

  1. why the value of 'sizeof(text)' is 7 whereas 'sizeof(text+2)' is 4
  2. what's the difference between 'text' and '&text'?

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays